package cn.aixuegao.web.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.aixuegao.common.base.controller.BaseController;
import cn.aixuegao.common.result.Response;
import cn.aixuegao.common.result.ResultMsg;
import cn.aixuegao.service.admin.StorageService;

@Controller
@RequestMapping("/file")
public class FileController extends BaseController{
	@Autowired
    private StorageService storageService;
	
	 //文件上传
    @PostMapping("/upload/")
    @ResponseBody
    public Response handleFileUpload(@RequestParam("file") MultipartFile file,
                                         RedirectAttributes redirectAttributes) {
        try {
        	String storeFileName = storageService.store(file);
            //String b =  MvcUriComponentsBuilder.fromMethodName(FileUploadController.class, "serveFile", storeFileName).build().toString();
            return resultData(ResultMsg.SUCCESS, storeFileName);
		} catch (Exception e) {
			return resultMsg(ResultMsg.FAILED);
		}
    }
}
